#!/usr/bin  python
# _*_ coding:utf-8 _*_
"""
----------------------------------
    File Name: appointments
    Description:
        Author: lhw
        Date: 2020/9/1
-----------------------------------
"""

import datetime
from rest_framework.views import APIView
from rest_framework.response import Response
from extensions.auth import JwtAuthorizationAuthentication
from Baby import models
from Baby.serializers import appointments
from Common.logger import app_log
from Common.response import resp


class AppointmentsView(APIView):
    # authentication_classes = [JwtAuthorizationAuthentication, ]

    def get(self, request, *args, **kwargs):
        try:
            baby_id = request.query_params.get("id", "")
            hospital = request.query_params.get("hospital", "")
            appointments_query = models.Appointments.objects.filter(babies=baby_id, hospital=hospital).all()
            appointments_data = appointments.AppointmentsModelSerializer(appointments_query, many=True).data
            return Response({
                'error_code': 0,
                'message': "Success",
                'data': appointments_data
            })
        except Exception as e:
            app_log.exception(e)
            return resp(-1, e.args)

    def post(self, request, *args, **kwargs):
        try:
            request_data = request.data
            appointments_ser = appointments.AppointmentsModelSerializer(data=request_data)
            appointments_ser.is_valid(raise_exception=True)
            appointments_obj = appointments_ser.save()
            return Response({
                "error_code": 0,
                "message": "Success",
                "data": appointments.AppointmentsModelSerializer(appointments_obj).data
            })
        except Exception as e:
            app_log.exception(e)
            return resp(-1, e.args)

    def put(self, request, *args, **kwargs):
        try:
            request_data = request.data
            appointments_id = request_data["id"]
            old_appointments_obj = models.Appointments.objects.filter(id=appointments_id).first()
            appointments_ser = appointments.AppointmentsModelSerializer(instance=old_appointments_obj,
                                                                        data=request_data, partial=True)
            appointments_ser.is_valid(raise_exception=True)
            appointments_obj = appointments_ser.save()
            return Response({
                "error_code": 0,
                "message": "Success",
                "data": appointments.AppointmentsModelSerializer(appointments_obj).data
            })
        except Exception as e:
            app_log.exception(e)
            return resp(-1, e.args)

    def delete(self, request, *args, **kwargs):
        try:
            appointments_id = request.GET.get("id")
            appointments_data = models.Appointments.objects.filter(id=appointments_id).first()
            if appointments_data:
                models.Appointments.objects.filter(id=appointments_id).delete()
                return Response({
                    "error_code": 0,
                    "message": "Success"
                })
            else:
                return resp(2005, {})
        except Exception as e:
            app_log.exception(e)
            return resp(-1, e.args)
